package cn.tedu.charging.order.rabbitmq;

import cn.tedu.charging.order.config.RabbitMQConfig;
import cn.tedu.charging.order.dao.repository.OrderRepository;
import cn.tedu.charging.order.pojo.po.OrderMQPO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class RabbitMQOderConsumer {
    @Autowired
    private OrderRepository orderRepository;
    @RabbitListener(queues = RabbitMQConfig.ORDER_DEAD_LATTER_QUEUE)
    public void consumeOrderDeadLatter(OrderMQPO orderMQPO, Message message, Channel channel) throws IOException {
        log.debug("消费死信队列消息:{}",orderMQPO);

        orderRepository.updateOrderStatusFromProcess2ExceptionEnd(orderMQPO.getOrderNo());

        //手动ack
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag,true);
    }
}
